package com.example.dell.workactivity.mvp;

import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.dell.adapter.VideoItemsSelectAdapter;
import com.example.dell.basic.BasicBasicActivity;
import com.example.dell.basicapplication.R;
import com.example.dell.been.VidoSelectBeen;

import java.util.ArrayList;
import java.util.List;

public class PopupWindowActivity extends BasicBasicActivity {

    private PopupWindow popupWindow;
    private View mViewById;
    private RecyclerView mRecyclerView;
    private VideoItemsSelectAdapter mAdapter;

    @Override
    protected int setContentSuccessView() {
        return R.layout.activity_popup_window;
    }

    @Override
    protected void initView(View view) {

        mViewById = view.findViewById(R.id.gone_view);
    }

    @Override
    protected void initData() {
        //这个永远是成功界面
        autoShowPager(true, "");
    }


    public void popupwindow(View view) {


        View contentView = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.video_select_layout, null);

        mRecyclerView = (RecyclerView)contentView.findViewById(R.id.mylist);


        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        final List<VidoSelectBeen> mBeen = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            VidoSelectBeen been = new VidoSelectBeen();
            been.setName("这是视频"+i*100);
            been.setType(i);
            mBeen.add(been);
        }
        int type = 5;
        mAdapter = new VideoItemsSelectAdapter(R.layout.video_select_text,mBeen,type);
        mRecyclerView.setAdapter(mAdapter);


        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        popupWindow = new PopupWindow(contentView, display.getWidth(), display.getHeight(), true);
        // 设置动画效果
        popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        //点击其他地方消失
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                closePopupWindow();
                return false;
            }
        });
        popupWindow.showAsDropDown(mViewById);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                System.out.println("选择的type是=="+mBeen.get(position).getName());
                closePopupWindow();
            }
        });

    }

    private void closePopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.alpha = 1f;
            getWindow().setAttributes(params);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdapter = null;
        mRecyclerView=null;
    }
}
